<?php

namespace app\controllers;

use app\models\User;
use Yii;
use app\models\UserApply;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;

/**
 */
class UserController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    /**
     * @return string
     */
    public function actionIndex()
    {
        $model = new User();
        $dataProvider = $model->search(Yii::$app->request->get());
        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'filterModel' => $model,
        ]);
    }

    /**
     * @return string
     */
    public function actionApply()
    {
        $model = new UserApply();
        $dataProvider = $model->search(Yii::$app->request->get());
        return $this->render('apply', [
            'dataProvider' => $dataProvider,
            'filterModel' => $model,
        ]);
    }

    /**
     * @return array
     */
    public function actionPass()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $id = \Yii::$app->request->post('id');
        $model = UserApply::findOne($id);
        $uid = Yii::$app->user->id;
        if ($model && $model->pass(intval($uid))) {
            return ['code' => 0];
        } else {
            return ['code' => 1];
        }
    }
}
